package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bwhx.eruler.bean.Baofei;
import com.bwhx.eruler.bean.OfflineOrder;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.service.BaofeiService;
import com.bwhx.eruler.service.RulerService;
/**
 * @author Cuilitang
 *
 */

@Controller
public class BaofeiController extends BaseController {
	
	@Autowired
	BaofeiService baofeiService;
	
	@Autowired
	RulerService rulerService;
	
	@ResponseBody
	@RequestMapping(value = "/page.saveBaofei.ashx")
	public String saveOOrder(Baofei baofei,HttpSession session) {
		try{
			Staff staff = (Staff) session.getAttribute("staff");
			baofei.setBftime(new Date());
			baofei.setBfcreatorid(staff.getsId());
			
			baofeiService.insert(baofei);
		
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.baofeiList.ashx")
	 public Map orderList(Integer page,Integer rows,Baofei baofei,HttpSession session) {
		
		Staff staff  = (Staff) session.getAttribute("staff");
		if(!staff.getsName().equals("admin")){
			baofei.setBfcreatorid(staff.getsId());

		}
		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,baofei);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<Baofei> list = baofeiService.list(p);
			Integer count = baofeiService.count(baofei);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.baofeidel.ashx")
	 public Map<String,String> OfflineOrderDel(@RequestParam String bfid){
		Map<String,String> resultMap = new HashMap<String, String>();
		resultMap.put("status", "1");
		baofeiService.del(bfid);
	
	    return resultMap;  
	  }
	
	@RequestMapping(value = "/page.bfdetails.ashx")
	 public ModelAndView OfflineOrderDetails(@RequestParam String bfid){
		Baofei baofei = baofeiService.get(bfid);
		
	    return new ModelAndView("/baofei/baofeidetails.jsp","baofei",baofei);  
	  }
	
	
	
	@ResponseBody
	@RequestMapping(value = "/page.processBaofei.ashx")
	 public Map<String,String> processOrder(@RequestBody String b,HttpSession session,Baofei baofei){
		log.info(b);
		Map<String,String> resultMap = new HashMap<>();
		
		
		
		Map<String, String> paramMap = new HashMap<String, String>();
		ArrayList<String> paramRSNList = new ArrayList<String>();
		List<Ruler> paramRulersList = new ArrayList<Ruler>();
		
		String subrsn="";
		String totalrsn="";
		
		
		String[] split = b.split("&");
		for (String string : split) {
			String[] keyValue = string.split("=");
			paramMap.put(keyValue[0], keyValue[1]);
		}
		
		
		//code和版本列表
		//bfid=3&bfpnumber=3&opcode_1=AA111111&opcode_2=AA111112&opcode_3=AA111113
		for(int i=1;i<=baofei.getBfpnumber();i++){
			String opc=paramMap.get("opcode_"+i);
			if(paramRSNList.contains(opc)){
				resultMap.put("status", "conflict");
				resultMap.put("RSN", opc);
				return resultMap;
			}else{
				paramRSNList.add(opc);
			}
			 
			
			Ruler availableRuler = rulerService.findAvailableRulerBySn(opc);
			
			if(null==availableRuler){
				resultMap.put("status", "unknown");
				resultMap.put("RSN", opc);
				return resultMap;
			}
			subrsn=opc+",";
			totalrsn+=subrsn;
			
			
			Ruler ruler = new Ruler();
			ruler.setRsn(opc);
			ruler.setrState(4);
			paramRulersList.add(ruler);
			
			
		}
		
		
		baofei.setBfpcodes(totalrsn.substring(0, totalrsn.length()-1));
		
		baofeiService.update(baofei,paramRulersList);
		
		resultMap.put("status", "1");
		
				
		return resultMap;  
	}
}
